<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <el-icon size="40"><OfficeBuilding /></el-icon>
        </div>
        <h2>团队角色评估系统</h2>
        <p>Team Role Assessment System V1.0</p>
      </div>

      <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-checkbox
            v-model="form.rememberPassword"
            size="large"
          >
            记住密码
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            style="width: 100%"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-tips">

        <p class="tip-note">提示：勾选"记住密码"可保存登录信息</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { OfficeBuilding } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

const loginForm = ref()
const loading = ref(false)

const form = reactive({
  username: '',
  password: '',
  rememberPassword: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 页面加载时恢复保存的登录信息
onMounted(() => {
  const savedUsername = localStorage.getItem('savedUsername')
  const savedPassword = localStorage.getItem('savedPassword')
  const rememberPassword = localStorage.getItem('rememberPassword') === 'true'

  if (rememberPassword && savedUsername && savedPassword) {
    form.username = savedUsername
    form.password = savedPassword
    form.rememberPassword = true
  }
})

const handleLogin = async () => {
  if (!loginForm.value) return

  try {
    const valid = await loginForm.value.validate()
    if (!valid) return

    loading.value = true

    const result = await userStore.login(form)

    if (result.success) {
      // 处理记住密码功能
      if (form.rememberPassword) {
        localStorage.setItem('savedUsername', form.username)
        localStorage.setItem('savedPassword', form.password)
        localStorage.setItem('rememberPassword', 'true')
      } else {
        localStorage.removeItem('savedUsername')
        localStorage.removeItem('savedPassword')
        localStorage.removeItem('rememberPassword')
      }
      ElMessage.success('登录成功')
      router.push('/dashboard/home')
    } else {
      ElMessage.error(result.message || '登录失败')
    }
  } catch (error) {
    ElMessage.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-tips {
  text-align: center;
  color: #999;
  font-size: 12px;
  border-top: 1px solid #eee;
  padding-top: 15px;
}

.login-tips p {
  margin: 2px 0;
}

.tip-note {
  color: #409eff !important;
  font-weight: 500;
}

.el-checkbox {
  color: #606266;
}
</style>
